package com.starblink.message.mymessage.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.basic.route.RoutePage
import com.starblink.android.basic.base.activity.BaseTVMActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.extension.navigation
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.post.PostUtils
import com.starblink.android.common.view.badgeview.BGABadgeImageView
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.message.databinding.ActivityMyMessageBinding
import com.starblink.message.messagefollower.ui.MessageFollowerActivity
import com.starblink.message.messagelikeorcomment.ui.MessageLikeOrCommentActivity

/**
 * 我的消息
 */
@Route(path = RoutePage.Message.MY_MESSAGE, extras = RoutePage.Extra.NEED_LOGIN)
class MyMessageActivity : BaseTVMActivity<ActivityMyMessageBinding, MyMessageVM>() {
    override fun initViewBinding(): ActivityMyMessageBinding =
        ActivityMyMessageBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //返回按钮
        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })

        viewBinding.layoutLike.click {
            viewModel.isLikeChecked = true
            MessageLikeOrCommentActivity.navigation(this, isLikeMessage = true)
        }
        viewBinding.layoutComments.click {
            viewModel.isCommentChecked = true
            MessageLikeOrCommentActivity.navigation(this, isLikeMessage = false)
        }
        viewBinding.layoutNewFollowers.click {
            viewModel.isNewFollowersChecked = true
            navigation(RoutePage.Message.MESSAGE_FOLLOWERS)
        }
        viewBinding.layoutSystemNotification.click {
            viewModel.isSystemNotificationChecked = true
            navigation(RoutePage.Message.MESSAGE_SYSTEM_NOTIFICATION)
        }
        viewBinding.layoutActivity.click {
            viewModel.isActivityChecked = true
            navigation(RoutePage.Message.MESSAGE_ACTIVITY)
        }

        viewModel.activityMsgLiveData.observe(this) {
            // 显示活动消息
            if (it.isNotEmpty()) {
                val item = it.first()
                if (item?.content != null && item?.pushTime != null) {
                    viewBinding.tvActivityContent.visibility = View.VISIBLE
                    viewBinding.tvActivityContent.text = item.content
                    if (item.pushTime is String) {
                        viewBinding.tvPushTime.visibility = View.VISIBLE
                        viewBinding.tvPushTime.text =
                            PostUtils.getTimeDiffDetailFriendly(item.pushTime as String)
                    }
                }
            }
        }
        viewModel.unreadCountLiveData.observe(this) {
            setBadgeImageView(viewBinding.ivBadgeLike, it.likedCount > 0)
            setBadgeImageView(viewBinding.ivBadgeNewFollowers, it.followAccountCount > 0)
            setBadgeImageView(viewBinding.ivBadgeComment, it.commentCount > 0)
            setBadgeImageView(viewBinding.ivBadgeSystemNotification, it.systemNotifyCount > 0)
            setBadgeImageView(viewBinding.ivBadgeActivity, it.systemMsgCount > 0)
        }
    }

    override fun onResume() {
        super.onResume()
        // Activity创建时加载数据
        // 从子页面返回时加载数据
        viewModel.loadData()
    }

    private fun setBadgeImageView(badgeImageView: BGABadgeImageView, show: Boolean) {
        if (show) {
            badgeImageView.badgeViewHelper.showCirclePointBadge()
        } else {
            badgeImageView.badgeViewHelper.hiddenBadge()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        // 如果进入过某个页面，关闭页面时，发送消息，通知[首页-我的]页面重新加载未读消息数量
        if (viewModel.isLikeChecked || viewModel.isNewFollowersChecked
            || viewModel.isCommentChecked || viewModel.isSystemNotificationChecked || viewModel.isActivityChecked
        ) {
            FlowBus.boolFlowBusNotify(FlowConst.UPDATE_MESSAGE_UNREAD_COUNT_EVENT)
        }
    }
}